package com.yjz.log.format.stack;

import com.yjz.log.format.ILogFormatter;

/**
 * 作用 : 堆栈信息
 * 作者 : yjz
 * 时间 : 2024/9/14 13:45
 */
public class StackTrackFormatter implements ILogFormatter<StackTraceElement[]> {
    @Override
    public String format(StackTraceElement[] stackTraceElements) {
        if (stackTraceElements == null || stackTraceElements.length == 0) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder(128);
            for (int i = 0, len = stackTraceElements.length; i < len; i++) {
                if (i == 0) {
                    sb.append("stackTrace: \n");
                }
                if (i != len - 1) {
                    sb.append("\t├ ");
                    sb.append(stackTraceElements[i].toString());
                    sb.append("\n");
                } else {
                    sb.append("\t└ ");
                    sb.append(stackTraceElements[i].toString());
                }
            }
            return sb.toString();
        }
    }
}
